//
//  MainViewController.swift
//  TTWeibo
//
//  Created by 唐磊guang on 16/7/3.
//  Copyright © 2016年 唐磊guang. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        addComposeButtton()
    }
    
    //MARK: --按钮点击事件
    func composeButtonClick() {
        print(#function)
    }
    
    //MARK: -- 私有方法
    private func addComposeButtton(){
        tabBar .addSubview(plusButton)
        let width = UIScreen.mainScreen().bounds.size.width / CGFloat(viewControllers!.count)
        
        plusButton.frame = CGRect(x: 0, y: 0, width: width, height: 49)
        plusButton.frame = CGRectOffset(plusButton.frame, 2 * width, 0)
    }
    
    private func addChildViewControllers() {
        //获取json文件路径
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        //从json文件路径下获得数据
        let data = NSData(contentsOfFile: path!)
        
        if data != nil
        {
            do{
                //将数据转化问swift对象
                let dicArr = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
                
                for dic in dicArr as! [[String : String]]{
                    addChildViewController(dic["vcName"]!, title:dic["title"]!, imageName: dic["imageName"]!)
                }
            }
            catch
            {
                addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
                addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
                addChildViewController("DiscoverTableViewController", title: "广场", imageName: "tabbar_discover")
                addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
            }
        }
    }
    private func addChildViewController(childController: String , title: String , imageName: String) {
        let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        
        let cls : AnyObject? = NSClassFromString(ns + "." + childController)
        
        let vcCls = cls as! UIViewController.Type
        
        let vc = vcCls.init()
//        print(ns,vcClass)
        
        
        let nav = UINavigationController(rootViewController: vc)
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        addChildViewController(nav)
        
    }
    //MARK: --懒加载
    lazy var plusButton: UIButton = {
        let button = UIButton()
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action:#selector(MainViewController.composeButtonClick) , forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()

}
